package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Prod;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProdDao {
	private DBHelp<Prod> db = new DBHelp<Prod>();
	public List<Prod> findAll() {
		String sql = "select prod_num,prod_name,prod_price,enable from t_prod";
		return db.executeQueryForList(sql, new ProdRowMapper());
	}
	
	private class ProdRowMapper implements RowMapper<Prod>{

		public Prod mapperRow(ResultSet rs) throws SQLException {
			Prod p = new Prod();
			p.setProd_num(rs.getString("prod_num"));
			p.setProd_name(rs.getString("prod_name"));
			p.setProd_price(rs.getString("prod_price"));
			p.setEnable(rs.getBoolean("enable"));
			return p;
		}
		
	}

	public void save(Prod p) {
		String sql="insert into t_prod(prod_num,prod_name,prod_price,enable)values(?,?,?,?)";
		db.executeSQL(sql, p.getProd_num(),p.getProd_name(),p.getProd_price(),p.isEnable());
		
	}

	public Prod findByNum(String num) {
		String sql = "select prod_num,prod_name,prod_price,enable from t_prod where prod_num=?";
		return db.executeQueryForObject(sql, new ProdRowMapper(), num);
	}

	public void del(String num) {
		String sql =" delete from t_prod where prod_num=?";
		db.executeSQL(sql, num);
	}

	public void edit(Prod prod) {
		String sql = "UPDATE t_prod SET prod_name=?,prod_price=?,ENABLE=? WHERE prod_num=?";
		db.executeSQL(sql, prod.getProd_name(),prod.getProd_price(),prod.isEnable(),prod.getProd_num());
		
	}

}
